package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.AddOrdersDto;
import com.codingfuture.erp_system.dto.OrdersDetailDto;
import com.codingfuture.erp_system.dto.OrdersDto;
import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.entity.Orders;
import com.codingfuture.erp_system.entity.Role;
import com.codingfuture.erp_system.entity.Supplier;
import com.codingfuture.erp_system.service.OrdersService;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v2")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 分页查询/模糊查询
     */
    @GetMapping("/orders/listByPage")
    public Result findByAll(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int rows, Orders orders) {
        Page<Orders> page1 = PageHelper.startPage(page, rows);
        List<Orders> ordersList = ordersService.findByAll(orders);
        for (Orders ordersDto : ordersList) {
            List<OrdersDetailDto> list1 = ordersService.findOrdersDetailDto(ordersDto.getUuid());
            ordersDto.setOrderDetailList(list1);
        }
        PageData<Orders> data = new PageData<>(ordersList, page1.getTotal());
        return Result.ok(data);
    }

    /**
     * 展示供应商姓名
     */
    @GetMapping("/orders/showsupplier")
    public Result showSupplier(Integer type) {
        List<Supplier> list = ordersService.showSupplier(type);
        return Result.OK(list);
    }

    /**
     * 查询商品信息
     */
    @GetMapping("/orders/listByPage1")
    public Result findByOrders() {
        List<OrdersDetailDto> list = ordersService.findByOrders();
        return Result.OK(list);
    }

    @PostMapping("/orders/add")
    public Result addOrdersDetail(@RequestBody AddOrdersDto addOrdersDto, HttpSession session) {
        Date date = new Date();
        List<OrdersDetailDto> ordersDetails = addOrdersDto.getOrderDetails();
        Integer supplieruuid = addOrdersDto.getSupplieruuid();
        OrdersDto ordersDto = new OrdersDto();
        Object name = session.getAttribute("name");
        Role role = ordersService.findByName(name);
        Integer id = role.getUuid();
        Double totalMoney = 0.0;
        for (OrdersDetailDto ordersDetail : ordersDetails) {
            totalMoney += ordersDetail.getMoney();
        }
        ordersDto.setTotalmoney(totalMoney);
        ordersDto.setSupplieruuid(supplieruuid);
        ordersDto.setCreatetime(date);
        ordersDto.setCreater(id);
        int num = ordersService.addById(ordersDto);
        long uuid = ordersDto.getUuid();
        for (OrdersDetailDto ordersDetail : ordersDetails) {
            ordersDetail.setEndtime(date);
            ordersDetail.setOrdersuuid(uuid);
        }
        int i = ordersService.addOrdersDetail(ordersDetails);
        if (num > 0 && i > 0) {
            return Result.ok();
        } else {
            return Result.error(201, "错误");
        }
    }

    /**
     * 采购订单确认--确认订单
     */
    @PostMapping("/orders/doStart")
    public Result doStart(Integer id) {
        Date date = new Date();
//        System.out.println(username);
//        System.out.println(date);
        Boolean result = ordersService.doStart(id, date);
        return result ? Result.ok() : Result.error(201, "错误");

    }

    @PostMapping("/orders/doCheck")
    public Result doCheck(Integer id) {
        Date date = new Date();
        Boolean result = ordersService.doCheck(id, date);
        return result ? Result.ok() : Result.error(201, "错误");
    }


}
